package cn.yuanqiao.web.controller.archive.wenjianzhengli;

import cn.yuanqiao.archive.appManager.domain.TenantCategory;
import cn.yuanqiao.archive.appManager.domain.TenantFields;
import cn.yuanqiao.archive.appManager.domain.TenantSysDictData;
import cn.yuanqiao.archive.appManager.service.ITenantCateMdataListService;
import cn.yuanqiao.archive.appManager.service.ITenantCategoryService;
import cn.yuanqiao.archive.wenjianzhengli.service.IWenjianzhengliService;
import cn.yuanqiao.archive.wenjianzhengli.service.impl.WenjianzhengliServiceImpl;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.poi.ListToExclUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import com.alibaba.fastjson2.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/archive/wjzl")
public class WenjianzhengliController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(WenjianzhengliController.class);
    @Autowired
    private IWenjianzhengliService wenjianzhengliService;
    @Autowired
    private WenjianzhengliServiceImpl wenjianzhengliServiceImpl;
    @Autowired
    private ITenantCateMdataListService tenantMdataListService;

    /**
     * 点击树节点获取列表数据
     * @author syd
     * @param json
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestBody JSONObject json)
    {
        startPage();
        List<Map<String,Object>> list = wenjianzhengliService.queryListOfWjzl(json);
        return getDataTable(list);
    }

    /**
     * 点击树节点获取列表字段
     * @author syd
     * @param json
     * @return
     */
    @GetMapping("/getshowfield")
    public AjaxResult getshowfield(@RequestBody JSONObject json)
    {
        List<Map<String,String>> list = new ArrayList<>();
        String nodeType = json.getString("nodeType");
        if("f".equalsIgnoreCase(nodeType) || "t".equalsIgnoreCase(nodeType) || "n".equalsIgnoreCase(nodeType)){
            return success(list);
        }
        String categoryId = json.getString("categoryId");
        String menuId = json.getString("menuId");
        JSONObject jsonObject = new JSONObject();
        Integer finalType = wenjianzhengliServiceImpl.getFinalType(json);
        jsonObject.put("level",0==finalType?"jian":1==finalType?"juan":3==finalType?"xiangmu":"");
        jsonObject.put("categoryId",categoryId);
        jsonObject.put("menuId",menuId);
        list=tenantMdataListService.getshowfield(jsonObject);
        return success(list);
    }

    /**
     * 点击树节点获取查询字段
     * @author syd
     * @param json
     * @return
     */
    @GetMapping("/getqueryfield")
    public AjaxResult getqueryfield(@RequestBody JSONObject json)
    {
        List<Map<String,String>> list = new ArrayList<>();
        String nodeType = json.getString("nodeType");
        if("f".equalsIgnoreCase(nodeType) || "t".equalsIgnoreCase(nodeType) || "n".equalsIgnoreCase(nodeType)){
            return success(list);
        }
        String categoryId = json.getString("categoryId");
        String menuId = json.getString("menuId");
        Integer finalType = wenjianzhengliServiceImpl.getFinalType(json);
        list=tenantMdataListService.getqueryfield(Long.valueOf(categoryId),Long.valueOf(menuId),finalType);
        return success(list);
    }

    /**
     * 文件整理取消接收
     * @author syd
     * @param ids
     * @return
     */
    @PutMapping("/cancelAccept")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:cancelAccept')")
    @Log(title = "文件整理取消接收", businessType = BusinessType.UPDATE)
    public AjaxResult cancelAccept(@RequestParam("ids") String ids,@RequestParam("categoryId") Long categoryId){
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.cancelAccept(ids,categoryId));
    }

    /**
     * 文件整理导出
     * @author syd
     * @param response
     * @param
     */
    @PostMapping("/export")
    @Log(title = "文件整理导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('archive:wjzl:export')")
    public void export(HttpServletResponse response,@RequestParam("data1") String data1)
    {
        JSONObject json = JSONObject.parseObject(data1);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        Workbook wb = null;
        try {
            //获取表头以及数据
            String categoryId = json.getString("categoryId");
            String menuId = json.getString("menuId");
            Integer finalType = Integer.valueOf(json.getString("type"));
            List<Map<String, String>> getexportfield = tenantMdataListService.getexportfield(Long.valueOf(categoryId), Long.valueOf(menuId), finalType);
            Map<String, Object> map = wenjianzhengliService.getExportTitleAndBody(json,getexportfield);
            wb = ListToExclUtil.getWorkbookByList((List<String>)map.get("title"),(List<List<String>>)map.get("body"),"sheet",(Map<String, Map<String,String>>)map.get("dict"));
            wb.write(response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
            log.error("导出Excel异常{}", e.getMessage());
        }finally {
            if(wb!=null){
                IOUtils.closeQuietly(wb);
            }
        }
    }

    /**
     * 排案卷号和件号
     * @param json
     * @return
     */
    @PutMapping("/pieceNumber")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:pieceNumber')")
    @Log(title = "排案卷号/件号", businessType = BusinessType.UPDATE)
    public AjaxResult pieceNumber(@RequestBody JSONObject json){
        int i = wenjianzhengliService.pieceNumber(json);
        if(i==-1){
            throw new ServiceException("案卷中档号为空的不能排卷内档号");
        }
        if(i==-2){
            throw new ServiceException("件号重复不能生成档号");
        }
        if(i==-3){
            throw new ServiceException("档号不符合规则");
        }
        if(i==-4){
            throw new ServiceException("卷内件生成档号末位超出补零位数，请修改档号规则");
        }
        if(i==-5){
            throw new ServiceException("生成档号末位超出补零位数，请修改档号规则");
        }
        if(i==-6){
            throw new ServiceException("档号组成字段为空的不能生成档号");
        }
        return toAjax(i);
    }


    /**
     * 排案卷号和件号
     * @param json
     * @return
     */
    @PutMapping("/pieceNumber1")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:pieceNumber1')")
    @Log(title = "排案卷号/件号", businessType = BusinessType.UPDATE)
    public AjaxResult pieceNumber1(@RequestBody JSONObject json){
        int i = wenjianzhengliService.pieceNumber(json);
        if(i==-1){
            throw new ServiceException("案卷中档号为空的不能排卷内档号");
        }
        if(i==-2){
            throw new ServiceException("件号重复不能生成档号");
        }
        if(i==-3){
            throw new ServiceException("档号不符合规则");
        }
        if(i==-4){
            throw new ServiceException("卷内件生成档号末位超出补零位数，请修改档号规则");
        }
        if(i==-5){
            throw new ServiceException("生成档号末位超出补零位数，请修改档号规则");
        }
        if(i==-6){
            throw new ServiceException("档号组成字段为空的不能生成档号");
        }
        return toAjax(i);
    }

    /**
     * 排案卷号和件号
     * @param json
     * @return
     */
    @PutMapping("/pieceNumber2")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:pieceNumber2')")
    @Log(title = "排案卷号/件号", businessType = BusinessType.UPDATE)
    public AjaxResult pieceNumber2(@RequestBody JSONObject json){
        int i = wenjianzhengliService.pieceNumber(json);
        if(i==-1){
            throw new ServiceException("案卷中档号为空的不能排卷内档号");
        }
        if(i==-2){
            throw new ServiceException("件号重复不能生成档号");
        }
        if(i==-3){
            throw new ServiceException("档号不符合规则");
        }
        if(i==-4){
            throw new ServiceException("卷内件生成档号末位超出补零位数，请修改档号规则");
        }
        if(i==-5){
            throw new ServiceException("生成档号末位超出补零位数，请修改档号规则");
        }
        if(i==-6){
            throw new ServiceException("档号组成字段为空的不能生成档号");
        }
        return toAjax(i);
    }

    /**
     * 排卷内件档号
     * @param json
     * @return
     */
    @PostMapping("/pieceInnerNumber")
    @Log(title = "排卷内件档号", businessType = BusinessType.UPDATE)
    public AjaxResult pieceInnerNumber(@RequestBody JSONObject json){
        return toAjax(wenjianzhengliService.pieceInnerNumber(json));
    }

    /**
     * 清除案卷号、件号
     * @author syd
     */
    @PutMapping("/clearNumber")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:clearNumber')")
    @Log(title = "清除案卷号/件号", businessType = BusinessType.UPDATE)
    public AjaxResult clearNumber(@RequestBody JSONObject json){
        int i = wenjianzhengliService.clearNumber(json);
        if(i==-1){
            throw new ServiceException("所选数据未生成档号");
        }
        return toAjax(i);
    }

    /**
     * 清除案卷号、件号
     * @author syd
     */
    @PutMapping("/clearNumber1")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:clearNumber1')")
    @Log(title = "清除案卷号/件号", businessType = BusinessType.UPDATE)
    public AjaxResult clearNumber1(@RequestBody JSONObject json){
        int i = wenjianzhengliService.clearNumber(json);
        if(i==-1){
            throw new ServiceException("所选数据未生成档号");
        }
        return toAjax(i);
    }

    /**
     * 清除案卷号、件号
     * @author syd
     */
    @PutMapping("/clearNumber2")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:clearNumber2')")
    @Log(title = "清除案卷号/件号", businessType = BusinessType.UPDATE)
    public AjaxResult clearNumber2(@RequestBody JSONObject json){
        int i = wenjianzhengliService.clearNumber(json);
        if(i==-1){
            throw new ServiceException("所选数据未生成档号");
        }
        return toAjax(i);
    }

    /**
     * 获取档号规则著录项最后一节
     * @author syd
     * @param json
     * @return
     */
    @PostMapping("/getLastZlx")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:insertPieceNumber')")
    public AjaxResult getLastZlx(@RequestBody JSONObject json){
        Map<String, Object> lastZlx = wenjianzhengliService.getLastZlx(json);
        return success(lastZlx);
    }

    /**
     * 获取档号规则著录项最后一节
     * @author syd
     * @param json
     * @return
     */
    @PostMapping("/getLastZlx1")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:insertPieceNumber1')")
    public AjaxResult getLastZlx1(@RequestBody JSONObject json){
        Map<String, Object> lastZlx = wenjianzhengliService.getLastZlx(json);
        return success(lastZlx);
    }

    /**
     * 获取档号规则著录项最后一节
     * @author syd
     * @param json
     * @return
     */
    @PostMapping("/getLastZlx2")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:insertPieceNumber2')")
    public AjaxResult getLastZlx2(@RequestBody JSONObject json){
        Map<String, Object> lastZlx = wenjianzhengliService.getLastZlx(json);
        return success(lastZlx);
    }

    @PostMapping("/getLastZlx3")
    //@RequiresPermissions("archive:wjzl:insertPieceNumber3")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:insertPieceNumber3')")
    public AjaxResult getLastZlx3(@RequestBody JSONObject json){
        Map<String, Object> lastZlx = wenjianzhengliService.getLastZlx(json);
        return success(lastZlx);
    }
    /**
     * 手动排档号
     *
     * @param json
     * @return
     */
    @PutMapping("/insertPieceNumber")
    //@PreAuthorize("@ss.hasPermi('archive:wjzl:insertPieceNumber')")
    @Log(title = "手动排档号", businessType = BusinessType.UPDATE)
    public AjaxResult insertPieceNumber(@RequestBody JSONObject json){
        int i = wenjianzhengliService.insertPieceNumber(json);
        if(i==-1){
            throw new ServiceException("档号组成字段为空的不能生成档号");
        }
        return toAjax(i);
    }

    /**
     * 获取列表数据
     */
    @PostMapping("/getlistbytreenode")
    @Log(title = "获取列表数据", businessType = BusinessType.SELECT)
    public TableDataInfo getlistbytreenode(@RequestBody JSONObject json)
    {
        List<Map<String,Object>> list = wenjianzhengliService.getlistbytreenode(json);
        return getDataTable(list);
    }
    /**
     * 获取表头
     */
    @PostMapping("/getthbytreenode")
    public TableDataInfo getthbytreenode(@RequestBody JSONObject json)
    {
        startPage();
        List<Map<String,String>> list = wenjianzhengliService.getthbytreenode(json);
        return getDataTable(list);
    }
    /**
     * 获取查询项
     */
    @PostMapping("/getqueryfieldbytreenode")
    public TableDataInfo getqueryfieldbytreenode(@RequestBody JSONObject json)
    {
        startPage();
        List<Map<String,String>> list = wenjianzhengliService.getqueryfieldbytreenode(json);
        return getDataTable(list);
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete1")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete1')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete1(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete2")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete2')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete2(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete3")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete3')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete3(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete4")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete4')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete4(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete5")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete5')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete5(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete6")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete6')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete6(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete7")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete7')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete7(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete8")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete8')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete8(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    /**
     * 文件整理删除
     * @author syd
     * @param json
     * @return
     */
    @DeleteMapping("/delete9")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:delete9')")
    @Log(title = "文件整理删除", businessType = BusinessType.DELETE)
    public AjaxResult delete9(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    @DeleteMapping("/deleteDgd")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:deleteDgd')")
    @Log(title = "待归档删除", businessType = BusinessType.DELETE)
    public AjaxResult deleteDgd(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }

    @DeleteMapping("/deleteZxjs")
    //@RequiresPermissions("archive:wjzl:deleteZxjs")
    @Log(title = "在线接收卷内删除", businessType = BusinessType.DELETE)
    public AjaxResult deleteZxjs(@RequestBody JSONObject json){
        String ids = json.getString("ids");
        if(!StringUtils.isNotEmpty(ids)){
            throw new ServiceException("ids不能为空");
        }
        return toAjax(wenjianzhengliService.delete(json));
    }
}
